<?php

namespace App\Logics;


use App\Models\Goods;
use App\Models\GoodsCate;
use App\Models\User;
use App\Service\Tree;

class GoodsCateLogic extends Logic
{

    // 列表
    public function lists($options = [])
    {
        $opt  = $this->option();
        $page = $opt->getData('page', 1, 'intval');
        $rows = $opt->getData('rows', 15, 'intval');
        $pid  = $opt->getData('pid', 0, 'intval');
        $_is  = $opt->getData('is_propose', null, 'intval');

        // 参数
        $is_page = $options['is_page'] ?? 1;
        $_number = $options['get_number'] ?? 0;
        $_is     = is_null($_is) ? ($options['is_propose'] ?? null) : $_is;

        // 查询
        $where = [['status', '=', 1]];
        $cache = ['status' => 1];
        if($pid){
            $where[] = ['pid', '=', $pid];
            // 缓存
            $cache['pid'] = $pid;
        }
        if(!is_null($_is)){
            $where[] = ['is_propose', '=', $_is];
            // 缓存
            $cache['is_propose'] = $_is;
        }


        $model = new GoodsCate();
        if($is_page){
            $result = $model->listPageCache($cache, $where, $page, $rows, 'rank ASC');
        } else{
            $result = $model->getListCache($cache, $where, $_number, 'rank ASC');
        }
        if($result === false){
            return $this->_returnFail($model->getError());
        }


        return $result;
    }

    // 树状
    public function tree($options = [])
    {
        $opt    = $this->option();
        $isTree = $opt->getData('isTree', 0, 'intval');

        // 树状制作
        $where = [['status', '=', 1]];
        $cache = ['status' => 1];
        $model = new GoodsCate();
        // 绑定检测
        $lists = $model->getListCache($cache, $where, 0, 'rank ASC');
        if($lists === false){
            return $this->_returnFail($model->getError());
        }
        if(!empty($lists)){
            $tree = new Tree();
            if($isTree){
                $lists = $tree->toList($lists); // 树状
            } else{
                $lists = $tree->toTree($lists); // 树状
            }
        }


        //$total  = count($lists);
        //$result = ['total' => $total, 'page' => 1, 'rows' => $rows, 'list' => $lists,];

        return $lists;
    }


}

